package com.lc.app.androidfirst.userdefine.view;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.lc.app.androidfirst.R;

import java.util.Timer;
import java.util.TimerTask;

public class HandleMessageActivity extends AppCompatActivity {
    int[] images = new int[]{R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4,R.drawable.image5};
    private int currentImage = 0;

    private int id = 0x1123;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image);
        LinearLayout layout = (LinearLayout) findViewById(R.id.image_root);
        final ImageView imageView = new ImageView(this);
        imageView.setImageResource(images[0]);
        layout.addView(imageView);

        final Handler handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what == id){
                    imageView.setImageResource(images[currentImage++]);
                    if(currentImage >= 4){
                        currentImage = 0;
                    }
                }
            }
        };

        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                Message msg = new Message();
                msg.what = id;
                handler.sendMessage(msg);
            }
        },0,800);

    }
}
